package com.xys.auth.controller;

import com.xys.auth.bean.Permission;
import com.xys.auth.bean.Role;
import com.xys.auth.service.IPermissionService;
import com.xys.auth.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.lang.management.MonitorInfo;
import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    IRoleService roleService;

    @Autowired
    IPermissionService permissionService;

    @RequestMapping("findAll.do")
    public ModelAndView findAll(){

        ModelAndView modelAndView = new ModelAndView();

        List<Role> roles = roleService.findAll();
        modelAndView.setViewName("role-list");
        modelAndView.addObject("roleList",roles);

        return modelAndView;
    }

    @RequestMapping("/save.do")
    public String save(Role role){
       roleService.save(role);

       return "redirect:findAll.do";
    }

    @RequestMapping("/findRoleByIdAndAllPermission.do")
    public ModelAndView findRoleByIdAndAllPermission(String id){
        ModelAndView modelAndView = new ModelAndView();

        Role role = new Role();
        role.setId(id);
        List<Permission> permissionList = permissionService.findOtherPermission(id);
        modelAndView.setViewName("role-permission-add");

        modelAndView.addObject("role",role);
        modelAndView.addObject("permissionList",permissionList);
        return modelAndView;
    }

    @RequestMapping("/addRoleToUser.do")
    public String addPermission(String roleId,String [] ids){

        roleService.addPermissionToRole(roleId ,ids);

        return "redirect:findAll.do";

    }


    @RequestMapping("/findById.do")
    public ModelAndView findById(String id){
        Role role = roleService.findById(id);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("role-show");
        modelAndView.addObject("role",role);
        return modelAndView;
    }

}
